package com.hebeu.device.service.impl;

import com.github.pagehelper.PageHelper;
import com.hebeu.device.dao.DClientMapper;
import com.hebeu.device.pojo.DClient;
import com.hebeu.device.pojo.DClientExample;
import com.hebeu.device.service.DClientService;
import com.hebeu.device.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 王煜
 * @date 2019-6-17
 */

@Service
public class DClientServiceImpl implements DClientService {

    @Resource
    DClientMapper dClientMapper;

    @Override
    public List<DClient> getDClients(DClient dClient) {
        PageHelper.startPage(dClient.getPageNo(),dClient.getPageSize());
        DClientExample dClientExample = new DClientExample();
        DClientExample.Criteria criteria = dClientExample.createCriteria();
        if(StringUtils.isNotEmpty(dClient.getcName())){
            System.out.println("lallalalallalalla");
            criteria.andCNameLike("%"+dClient.getcName()+"%");}
        if(dClient.getActive() != null)
            criteria.andActiveEqualTo(dClient.getActive());


//        //增加客户姓名查询(tzz)
//        if(dClient.getcName()!=null){
//            criteria.andCNameEqualTo(dClient.getcName());
//        }


        List<DClient> dClients = dClientMapper.selectByExample(dClientExample);
        return dClients;
    }

    @Override
    public List<DClient> getAll(DClient dClient) {
        DClientExample dClientExample = new DClientExample();
        return dClientMapper.selectByExample(dClientExample);
    }

    @Override
    public DClient getDClientById(int cId) {
        return dClientMapper.selectByPrimaryKey(cId);
    }

    @Override
    public int add(DClient dClient) {
        dClient.setCreateTime(new Date());
        return dClientMapper.insertSelective(dClient);
    }

    @Override
    public int del(DClient dClient) {
        dClient.setCreateTime(new Date());
        return dClientMapper.updateByPrimaryKeySelective(dClient);
    }

    @Override
    public int update(DClient dClient) {
        dClient.setCreateTime(new Date());
        return dClientMapper.updateByPrimaryKeySelective(dClient);
    }


    //查询客户信息的实现方法（tzz）
    @Override
    public List<DClient> findDClientList(DClient dClient){
        PageHelper.startPage(dClient.getPageNo(),dClient.getPageSize());
        DClientExample dClientExample=new DClientExample();
        DClientExample.Criteria criteria = dClientExample.createCriteria();
        if(dClient.getcName()!=null && !"".equals(dClient.getcName())){
            System.out.println("输出：" + dClient.getcName());
            criteria.andCNameEqualTo(dClient.getcName());
        }
        if(dClient.getActive() != null&&dClient.getActive()!=0){
            criteria.andActiveEqualTo(dClient.getActive());
        }

        return dClientMapper.selectByExample(dClientExample);
    }
}
